package com.bocai.service.impl;

import com.bocai.bean.TSalesOutMoneyInfo;
import com.bocai.bean.TSalesUser;
import com.bocai.mapper.TSalesOutMoneyInfoMapper;
import com.bocai.mapper.TSalesUserMapper;
import com.bocai.service.ISalesService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.MD5Utils;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SalesServiceImpl implements ISalesService {
    @Autowired
    private TSalesUserMapper salesUserMapper;
    @Autowired
    private TSalesOutMoneyInfoMapper outMoneyInfoMapper;

    @Override
    public List<TSalesUser> selectSales(TSalesUser tSalesUser) {
        return null;
    }

    @Override
    public TSalesUser selectSaleForPhone(String phone) {
        return this.salesUserMapper.selectByPhone(phone);
    }

    @Override
    public int insertSale(TSalesUser tSalesUser) {
        return salesUserMapper.insertSelective(tSalesUser);
    }

    @Override
    public int updateSale(TSalesUser tSalesUser) {
        return salesUserMapper.updateByPrimaryKeySelective(tSalesUser);
    }

    @Override
    public int updateCheck(TSalesOutMoneyInfo moneyInfo) {
        TSalesUser tSalesUser = salesUserMapper.selectByPrimaryKey(moneyInfo.getDistributorId());
        if (null == tSalesUser) {
            return 0;
        } else if (tSalesUser.getSalesStatus() == 0) {
            return outMoneyInfoMapper.updateByPrimaryKeySelective(moneyInfo);
        } else {
            return -1;
        }
    }

    @Override
    public int updateBackMoney(TSalesOutMoneyInfo moneyInfo) {
        TSalesUser tSalesUser = salesUserMapper.selectByPrimaryKey(moneyInfo.getDistributorId());
        if (null == tSalesUser) {
            return 0;
        } else if (tSalesUser.getSalesStatus() == 0) {
            tSalesUser.setSalesMoney(moneyInfo.getCashMoney());//即将退回的钱
            return salesUserMapper.updateSalesAddMoney(tSalesUser); //修改用户余额
        } else {
            return -1;
        }
    }

    @Override
    public int deleteSale(int salesId) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> selectSalesList(TSalesUser salesUser) {
        List<Map<String, Object>> lists = salesUserMapper.selectSalesList(salesUser);
        for (int i = 0; i < lists.size(); i++) {
            BigDecimal outMoney = outMoneyInfoMapper.selectMoneyByKey((Integer) lists.get(i).get("salesId"));
            lists.get(i).put("outMoney", outMoney);
        }
        return lists;
    }

    @Override
    public List<Map<String, Object>> selectSalesDetail(Integer userId) {
        return salesUserMapper.selectSalesDetail(userId);
    }

    @Override
    public ResponseData loginSaleMethod(String phone, String pwd, ResponseData data) {
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{phone, pwd});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        TSalesUser salesUser = this.selectSaleForPhone(phone);

        if (null == salesUser) {
            data.setCodeEnum(CodeEnum.CODE_10003);
            return data;
        }

        if (!salesUser.getSalesPassword().equals(MD5Utils.md5Password(pwd))) {
            data.setCodeEnum(CodeEnum.CODE_10002);
            return data;
        }

        data.setCodeEnum(CodeEnum.SUCCESS);
        data.setResults(salesUser);
        return data;
    }

    @Override
    public List<Map<String, Object>> selectOutMoney(Integer salesId) {

        return outMoneyInfoMapper.selectOutMoney(salesId);
    }

    @Override
    public Map<String, Object> selectMoneys() {
        BigDecimal outMoneyAll = outMoneyInfoMapper.selectMoneyByKey(null);
        BigDecimal inMoneyAll = salesUserMapper.selectBalanceMoneyByKey();
        List<Map<String, Object>> lists = outMoneyInfoMapper.selectMoneyLists();
        Map<String, Object> map = new HashMap<>();
        map.put("outMoney", outMoneyAll);
        map.put("inMoneyAll", inMoneyAll);
        map.put("getMoneyLists", lists);
        return map;
    }

    @Override
    public TSalesOutMoneyInfo selectOutMoneyDetail(Integer putForwardId) {

        return outMoneyInfoMapper.selectByPrimaryKey(putForwardId);
    }

    @Override
    public Map<String, Object> selectSalesDetails(Integer salesId) {
        Map<String, Object> map = salesUserMapper.selectSalesDetails(salesId);
        if (map != null)
            map.put("outMoney", outMoneyInfoMapper.selectMoneyByKey(salesId));
        return map;
    }

    @Override
    public List<Map<String, Object>> selectSalesUserList(Map map) {
        return salesUserMapper.selectSalesUserList(map);
    }
}
